package com.experiment.aicommunityback.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.experiment.aicommunityback.entity.Result;
import com.experiment.aicommunityback.entity.User;
import com.experiment.aicommunityback.service.UserService;
import com.experiment.aicommunityback.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/login")
@CrossOrigin()
public class LoginController {
    private UserService userService;

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }

    //    将name,password 打包成json文件发给后端localhost:8080/Login/selectByName
//    记得所有的都返回Result
    @PostMapping("/log")
    public Result login(@RequestBody User user) {
        String name = user.getName();
        String password = user.getPassword();
        System.out.println(name);
        return userService.getLog(name, password);
    }

    @GetMapping("/check_Token")
    public Boolean checkToken(HttpServletRequest request){
        String token=request.getHeader("token");
        return JwtUtils.check_token(token);
    }

    @PostMapping("/getCodeOfUser")
    public String getCodeOfUser(@RequestBody String userId) {
        System.out.println(userId);
        Object parse = JSON.parse(userId);
        JSONObject jsonObject=(JSONObject) parse;
        Object o = jsonObject.get("id");
        return userService.queryRoleCode((Integer) o);
    }
}
